import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/screenutil_init.dart';
import 'package:provider/provider.dart';

import './model/initapp_model.dart';
import './model/theme_model.dart';
import './router/router.dart';
import './utils/tools/localstorage_tool.dart';
import 'common/custom_icons.dart';

void main() async {
  // 确保WidgetsFlutterBinding被初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化EasyLoading设置
  EasyLoading.instance
    ..indicatorType = EasyLoadingIndicatorType.circle
    ..errorWidget = Icon(Icons.error,color: Colors.white,size: 45)
    ..successWidget = Icon(CustomIcons.success,color: Colors.white,size: 45)
    ..textStyle = TextStyle(fontSize: 14,color: Colors.white);

  // 获取主题信息
  String themeName = await LocalStorage.getStringItem('themeContext');

  // 强制竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
    runApp(
      MultiProvider(
        providers: [//"ThemeType.dark"
          ChangeNotifierProvider(create: (context) => InitAppModel()),
          ChangeNotifierProvider(create: (context) => ThemeModel(themeName)),
        ],
        child: MyApp(1.0),
        //child: MyApp(fontScale != null ? fontScale : 1.0),
      )
    );
  });

}


class MyApp extends StatelessWidget {
  double fontScale;

  MyApp(double fontScale) {
    this.fontScale = fontScale;
  }

  // 汉化配置
  Iterable<LocalizationsDelegate<dynamic>> sinicizationConfig = [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ];


  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1334),
      child: MaterialApp(
        localizationsDelegates: sinicizationConfig,
        supportedLocales: [Locale("zh", "CN")],
        builder: EasyLoading.init(
          builder: (context,widget){
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: fontScale),
              child: widget,
            );
          }
        ),
        //navigatorObservers: [BotToastNavigatorObserver()],
        theme: Provider.of<ThemeModel>(context, listen: true).themeData,
        debugShowCheckedModeBanner: false,
//        initialRoute: "/demo",
        initialRoute: "/loginview",
        onGenerateRoute: onGenerateRoute,
      ),
    );
  }
}





